package cn.iocoder.yudao.module.forward.controller.admin.datasourceinfo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.forward.controller.admin.datasourceinfo.vo.*;
import cn.iocoder.yudao.module.forward.dal.dataobject.datasourceinfo.DataSourceInfoDO;
import cn.iocoder.yudao.module.forward.service.datasourceinfo.DataSourceInfoService;

@Slf4j
@Tag(name = "管理后台 - 信息源配置")
@RestController
@RequestMapping("/forward/data-source-info")
@Validated
public class DataSourceInfoController {

    @Resource
    private DataSourceInfoService dataSourceInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建信息源配置")
    @PreAuthorize("@ss.hasPermission('forward:data-source-info:create')")
    public CommonResult<Integer> createDataSourceInfo(@Valid @RequestBody DataSourceInfoSaveReqVO createReqVO) {
        return success(dataSourceInfoService.createDataSourceInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新信息源配置")
    @PreAuthorize("@ss.hasPermission('forward:data-source-info:update')")
    public CommonResult<Boolean> updateDataSourceInfo(@Valid @RequestBody DataSourceInfoSaveReqVO updateReqVO) {
        dataSourceInfoService.updateDataSourceInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除信息源配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('forward:data-source-info:delete')")
    public CommonResult<Boolean> deleteDataSourceInfo(@RequestParam("id") Integer id) {
        dataSourceInfoService.deleteDataSourceInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得信息源配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('forward:data-source-info:query')")
    public CommonResult<DataSourceInfoRespVO> getDataSourceInfo(@RequestParam("id") Integer id) {
        DataSourceInfoDO dataSourceInfo = dataSourceInfoService.getDataSourceInfo(id);
        return success(BeanUtils.toBean(dataSourceInfo, DataSourceInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得信息源配置分页")
    @PreAuthorize("@ss.hasPermission('forward:data-source-info:query')")
    public CommonResult<PageResult<DataSourceInfoRespVO>> getDataSourceInfoPage(@Valid DataSourceInfoPageReqVO pageReqVO) {
        PageResult<DataSourceInfoDO> pageResult = dataSourceInfoService.getDataSourceInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DataSourceInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出信息源配置 Excel")
    @PreAuthorize("@ss.hasPermission('forward:data-source-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDataSourceInfoExcel(@Valid DataSourceInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DataSourceInfoDO> list = dataSourceInfoService.getDataSourceInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "信息源配置.xls", "数据", DataSourceInfoRespVO.class,
                        BeanUtils.toBean(list, DataSourceInfoRespVO.class));
    }

    @GetMapping("/select")
    @Operation(summary = "获得信息源配置")
    @Parameter(name = "sourceId", description = "信息源ID", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('forward:data-source-info:query')")
    public CommonResult<DataSourceInfoRespVO> getDataSourceInfoBySourceId(@RequestParam("sourceId") Integer sourceId) {
        DataSourceInfoDO dataSourceInfo = dataSourceInfoService.getDataSourceInfoBySourceId(sourceId);
        return success(BeanUtils.toBean(dataSourceInfo, DataSourceInfoRespVO.class));
    }
}